package ru.compscicenter.uno.log;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: SLeeP
 * Date: 27.02.12
 * Time: 19:36
 */
public class Logger {
    private static Logger INSTANCE;
    public static final String DEFAULT_FILE = "errors.log";
    private static final long SIZE_LIMIT = 52428800; // bytes

    public static enum Mode {
        ERROR,
        ACTION
    }

    public static enum Output {
        FILE,
        CONSOLE
    }

    private static Mode mode;
    private static Output output;
    private static String file = DEFAULT_FILE;

    public Logger(Mode mode) {
        if (INSTANCE == null) {
            Logger.mode = mode;
            Logger.output = Output.CONSOLE;
        }
    }

    public Logger(Mode mode, String file) {
        if (INSTANCE == null) {
            Logger.mode = mode;
            Logger.output = Output.FILE;
            this.file = file;
        }
    }

    public static Logger getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Logger(Mode.ERROR, Logger.DEFAULT_FILE);
        }
        return INSTANCE;
    }

    public static void write(String str, Mode mode) {
        File f = new File(file);
        //System.out.println(f.length());
        if (f.length() > SIZE_LIMIT) {
            String file2 = "old_" + file;
            File f2 = new File(file2);

            if (f2.exists()) {
                f2.delete();
            }

            if (f.renameTo(f2)) {
                Logger.write("Error file renamed to " + file2, Mode.ACTION);
            } else {
                Logger.write("Failed to rename error file renamed to " + file2, Mode.ACTION);
            }
        }

        if (Logger.mode.equals(mode) || Logger.mode.equals(Mode.ACTION)) {
            str = formatActionLogUnit(str, mode);
            PrintWriter pw = null;
            if (Logger.output.equals(Output.FILE)) {                                
                try {
                    pw = new PrintWriter(new FileWriter(file, true), true);
                    pw.write(str);
                    pw.flush();
                } catch (IOException e) {
                    System.out.println(str);
                    System.out.flush();
                } finally {
                    if (pw != null) {
                        pw.close();
                    }
                }
            } else {
                System.out.println(str);
                System.out.flush();
            }
        }
    }

    private static String formatActionLogUnit(String str, Mode m) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        if (m.equals(Mode.ERROR)) {
            return m + "  " + dateFormat.format(date) + " " + str + "\n";
        } else {
            return m + " " + dateFormat.format(date) + " " + str + "\n";
        }
    }

    public static Mode getMode() {
        return Logger.mode;
    }

    public static Output getOutput() {
        return Logger.output;
    }
}
